package Map.HashMap集合存储学生对象并遍历;

import java.util.HashMap;

import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        Map<String, Student> student = new HashMap<String, Student>();
        Student s1 = new Student("姜恒星", 30);
        Student s2 = new Student("姜恒", 20);
        Student s3 = new Student("姜", 10);

        student.put("学生一", s1);
        student.put("学生二", s2);
        student.put("学生三", s3);

        //利用键找值
        Set<String> keySet = student.keySet();
        for (String key : keySet) {
            Student value = student.get(key);
            System.out.println(key + "," + value.getName() + "," + value.getAge());
        }

        //键值对找键和值
        Set<Map.Entry<String, Student>> stu = student.entrySet();
        for (Map.Entry<String, Student> s : stu) {
            String key = s.getKey();
            Student value = s.getValue();
            System.out.println(key + "," + value.getName() + "," + value.getAge());
        }
    }
}
